CREATE PROCEDURE RightRole_FindAll
AS
BEGIN
    Select 
    RightRoleRowId,
    ApplicationId,
    RightName,
    Role
    from RightRole
END
GO

CREATE PROCEDURE RightRole_FindById
@RightRoleRowId int
AS
BEGIN
    Select * from RightRole where RightRoleRowId = @RightRoleRowId
END
GO

CREATE PROCEDURE RightRole_FindByName
@name nvarchar(50)
AS
BEGIN
    Select * from RightRole where RightRoleRowId = @name
END
GO

CREATE PROCEDURE RightRole_Insert
        @RightRoleRowId int ,
        @ApplicationId int ,
        @RightName nvarchar (100),
        @Role nvarchar (100)
AS
BEGIN
INSERT INTO RightRole
(
        RightRoleRowId,
        ApplicationId,
        RightName,
        Role
)
VALUES
(
        @RightRoleRowId,
        @ApplicationId,
        @RightName,
        @Role
)
select @RightRoleRowId = @@IDENTITY
END
GO

CREATE PROCEDURE RightRole_Update
        @RightRoleRowId int ,
        @ApplicationId int ,
        @RightName nvarchar (100),  
        @Role nvarchar (100),
AS
BEGIN
UPDATE  RightRole SET
    ApplicationId = @ApplicationId,
    RightName = @RightName,
    Role = @Role
    WHERE RightRoleRowId = @RightRoleRowId
END
GO

CREATE PROCEDURE RightRole_Delete
    @RightRoleRowId int
AS
BEGIN
    DELETE FROM RightRole where RightRoleRowId = @RightRoleRowId
END
GO

CREATE PROCEDURE RightRole_Paging
    @PageSize int, 
    @PageNumber int
    AS 
    declare @RowStart int
    declare @RowEnd int
    if @PageNumber > 0 
    BEGIN	
    set @PageNumber = @PageNumber - 1
    set @RowStart = @PageSize * @PageNumber + 1;
    set @RowEnd = @RowStart  + @PageSize - 1;
    SELECT TotalRowCount = COUNT(RightRoleRowId) FROM RightRole;
    WITH MyTable as 
    (
    SELECT * ,  ROW_NUMBER() OVER (order by SortIndex) as RowNumber From  RightRole
    )
    SELECT * from MyTable where RowNumber >= @RowStart and RowNumber <= @RowEnd
END
GO

CREATE proc RightRole_SetActive
@RightRoleRowId int
AS
declare @Type bit
SELECT @Type =  IsActive from RightRole where RightRoleRowId = @RightRoleRowId
BEGIN
IF(@Type = 1)
Update RightRole set IsActive = 0 where RightRoleRowId = @RightRoleRowId
else
Update RightRole set IsActive = 1 where RightRoleRowId = @RightRoleRowId
END
GO

CREATE proc RightRole_SetSortIndex
@RightRoleRowId int, 
@Type int
AS
declare @CurrentSort int
select @CurrentSort = SortIndex from  RightRole where RightRoleRowId = @RightRoleRowId
BEGIN
IF(@Type = 1)
Update RightRole set SortIndex =  @CurrentSort + 1 where RightRoleRowId = @RightRoleRowId
else
Update RightRole set SortIndex =  @CurrentSort - 1 where RightRoleRowId = @RightRoleRowId
END
GO

CREATE PROCEDURE RightRole_RemoveSelectedItem
    @RightRoleRowId int
AS
BEGIN
    UPDATE RightRole SET IsDeleted = 1 WHERE RightRoleRowId = @RightRoleRowId
END
GO

CREATE PROCEDURE RightRole_RestoreSelectedItem
    @RightRoleRowId int
AS
BEGIN
    UPDATE RightRole SET IsDeleted = 0 WHERE RightRoleRowId = @RightRoleRowId
END
GO

CREATE PROCEDURE RightRole_RemoveAll
    @RightRoleRowId int
AS
BEGIN
    UPDATE RightRole SET IsDeleted = 1
END
GO

CREATE PROCEDURE RightRole_RestoreAll
    @RightRoleRowId int
AS
BEGIN
    UPDATE RightRole SET IsDeleted = 0
END
GO

